/**
 * LocaleMapManager.java
 * Product:BaseLayer
 * Version:1.0
 * Copyright 2009 by DNE
 * All Rights Reserved.
 */
package com.site.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Element;

import com.site.util.constant.Constants;


/**
 * 提供对中英文资源文件管理
 * 
 */
public class LocaleMapManager {

	// 配置文件名
	private String file_Name = "LocaleResource.xml";

	// 配置文件路径
	private String file_Path = PropertiesFactory.configPath+"message/";

	// CodeXml存放容器 CodeMap_(下拉列表框中英文)
	private Map<String, Map> codeMap_ = null;

	// MessageXml 存放容器 MessageMap_(提示信息中英文)
	private Map<String, Map> messageMap_ = null;

	// ScreenItemXml 存放容器 ScreenItemMap(页面label信息中英文)
	private Map<String, Map> screenItemMap_ = null;//  

	// 读取xml工具
	private XmlUtil xmlutil = null;

	// 文件列表
	private List<Element> fileNode = null;

	// 语言
	private String lang_;

	/**
	 * 构造函数
	 */
	public LocaleMapManager() throws Exception {
		loadDefaultXML();
	}
	/**
	 * 构造函数
	 */
	public LocaleMapManager(String path) throws Exception {
		if(!"".equalsIgnoreCase(path)){
			this.file_Path=path;
		}
		loadDefaultXML();
	}

	/**
	 * 加载XML文件
	 * 
	 * @param path
	 */

	public void loadDefaultXML() throws IllegalAccessException {
		String path = file_Path + file_Name;
		File file = new File(path);
		if (!file.exists()) {
			throw new IllegalArgumentException("Error:" + path
					+ " is not found");
		}
		if (xmlutil == null) {
			xmlutil = new XmlUtil();
		}
		Element root = xmlutil.getRootElement(path);
		lang_ = root.element("default").getTextTrim();
		fileNode = root.elements("locale");
	}

	/**
	 * 获取MessageManager
	 * 
	 * @return
	 */
	public Map getMessageManager() {
		for (int i = 0; i < fileNode.size(); i++) {
			String lang = ((Element) fileNode.get(i)).attributeValue("lang");
			List ele_msg = ((Element) fileNode.get(i)).elements("message-file");
			for (Iterator<Element> iterator = ele_msg.iterator(); iterator
					.hasNext();) {
				Element msg_ = iterator.next();
				String messageFile = msg_.getTextTrim();
				Element nodeList = xmlutil.getRootElement(file_Path
						+ messageFile);
				if (nodeList != null && nodeList.elements().size() > 0) {
					setMessageMap(lang, nodeList.elements());
				}
			}
		}
		return messageMap_;
	}

	/**
	 * 取Message.xml內容存入MessageManager
	 * 
	 * @return
	 */
	public void setMessageMap(String lang, List<Element> nodeList) {
		MessageMap messageMap = null;
		if (messageMap_ != null && messageMap_.containsKey(lang)) {
			messageMap = (MessageMap) messageMap_.get(lang);
		} else {
			messageMap = new MessageMapUtil(lang);
		}
		for (Iterator<Element> iterator = nodeList.iterator(); iterator
				.hasNext();) {
			Element element = iterator.next();
			Message msg = new Message(element.attributeValue("key"), element
					.attributeValue("dspmsg"), element.attributeValue("type"));
			messageMap.put(msg.get_key(), msg);
		}
		if (messageMap_ == null) {
			messageMap_ = new HashMap<String, Map>();
		}
		messageMap_.put(lang, messageMap);
	}

	/**
	 * 获取CodeManager
	 * 
	 * @return
	 */
	public Map getCodeManager() {
		for (int i = 0; i < fileNode.size(); i++) {
			String lang = ((Element) fileNode.get(i)).attributeValue("lang");
			List code_msg = ((Element) fileNode.get(i)).elements("code-file");
			for (Iterator<Element> iterator = code_msg.iterator(); iterator
					.hasNext();) {
				Element msg_ = iterator.next();
				String codeFile = msg_.getTextTrim();
				Element nodeList = xmlutil.getRootElement(file_Path + codeFile);
				if (nodeList != null && nodeList.elements().size() > 0) {
					setCodeMap(lang, nodeList.elements());
				}
			}
		}
		return codeMap_;
	}

	/**
	 * 取Code.xml內容存入CodeManager
	 * 
	 * @return
	 */
	public void setCodeMap(String lang, List<Element> nodeList) {
		CodeMap codeMap = null;
		if (codeMap_ != null && codeMap_.containsKey(lang)) {
			codeMap = (CodeMap) codeMap_.get(lang);
		} else {
			codeMap = new CodeMapUtil(lang);
		}
		for (Iterator<Element> iterator = nodeList.iterator(); iterator
				.hasNext();) {
			Element element = iterator.next();
			String key_value = element.attributeValue("name");
			List<Element> nodes_xml_ = element.elements();
			List<Code> listNode = new ArrayList<Code>();
			for (Iterator<Element> iterator3 = nodes_xml_.iterator(); iterator3
					.hasNext();) {
				Element element2 = (Element) iterator3.next();
				Code codeXml = new Code();
				codeXml.setCode(element2.attributeValue("code"));
				codeXml.setText(element2.getTextTrim());
				listNode.add(codeXml);
			}
			codeMap.put(key_value, listNode);
		}
		if (codeMap_ == null) {
			codeMap_ = new HashMap<String, Map>();
		}
		codeMap_.put(lang, codeMap);

	}



	/**
	 * 重新加载
	 * 
	 * @throws IllegalAccessException
	 */
	public void refresh() throws IllegalAccessException {
		loadDefaultXML();
	}

	/**
	 * 加载 MessageMap,CodeMap,ScreenItemMap
	 * 
	 * @return
	 */
	public Map initMap() {
		Map map = new HashMap();
		map.put(Constants.COMMON_MESSAGE_XML, getMessageManager());
		map.put(Constants.COMMON_CODE_XML, getCodeManager());
		map.put(Constants.COMMON_SCREENITEM_XML, getScreenItemManager());
		return map;
	}
	
	/**
	 * 获取ScreenItemManager
	 * 
	 * @return
	 */
	public Map getScreenItemManager() {
		for (int i = 0; i < fileNode.size(); i++) {
			String lang = ((Element) fileNode.get(i)).attributeValue("lang");
			List screenitem_msg = ((Element) fileNode.get(i))
					.elements("screenitem-file");
			for (Iterator<Element> iterator = screenitem_msg.iterator(); iterator
					.hasNext();) {
				Element msg_ = iterator.next();
				String screenitemFile = msg_.getTextTrim();
				Element nodeList = xmlutil.getRootElement(file_Path
						+ screenitemFile);
				if (nodeList != null && nodeList.elements().size() > 0) {
					setScreenItemMap(lang, nodeList.elements());
				}
			}
		}
		return screenItemMap_;
	}
	
	/**
	 * 取ScreenItem.xml內容存入screenItemManager
	 * 
	 * @return
	 */
	public void setScreenItemMap(String lang, List<Element> nodeList) {
		ScreenItemMap screentItemMap = null;
		if (screenItemMap_ != null && screenItemMap_.containsKey(lang)) {
			screentItemMap = (ScreenItemMap) screenItemMap_.get(lang);
		} else {
			screentItemMap = new ScreenItemMapUtil(lang);
		}
		for (Iterator<Element> iterator = nodeList.iterator(); iterator
				.hasNext();) {
			Element element = iterator.next();
			ScreenItem screenitem = new ScreenItem(element
					.attributeValue("key"), element.attributeValue("dspmsg"));
			screentItemMap.put(screenitem.getKey(), screenitem);
		}
		if (screenItemMap_ == null) {
			screenItemMap_ = new HashMap<String, Map>();
		}
		screenItemMap_.put(lang, screentItemMap);
	}
}